package com.dshore.messagecenter.dao.warning;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.system.SysDict;
import com.dshore.messagecenter.model.topicqueue.MdTopic;
import com.dshore.messagecenter.model.warning.MrWarningProcessing;

@Repository
public class MrWarningProcessingDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	//表格数据
	public List<MrWarningProcessing> getPage(MrWarningProcessing mrWarningProcessing) {
		String stime=mrWarningProcessing.getStime();
		String etime=mrWarningProcessing.getEtime();
		List<Object> list=new ArrayList<>();
		StringBuffer sql=new StringBuffer("SELECT * from (SELECT A.*, rownum rn from (SELECT ID,ALARM_ITEM,ALARM_TIME,WARNING_THRESHOLD,COMPARISON_OPERATORS,ACTUAL_NUMERICAL,OBJ_TYPE,OBJ,PRINCIPAL,WARNING_TYPE,STATUS,DEAL_PEOPLE,DEAL_WAY,UNIT FROM T_MR_WARNING_PROCESSING WHERE DEL_FLAG=0");
		if(!StringUtils.isEmpty(mrWarningProcessing.getAlarmItem())) {
			sql.append(" AND ALARM_ITEM=?");
			list.add(mrWarningProcessing.getAlarmItem());
		}
		if(!StringUtils.isEmpty(mrWarningProcessing.getObjType())) {
			sql.append(" AND OBJ_TYPE=?");
			list.add(mrWarningProcessing.getObjType());
		}
		if(!StringUtils.isEmpty(mrWarningProcessing.getObj())) {
			sql.append(" AND OBJ=?");
			list.add(mrWarningProcessing.getObj());
		}
		if(!StringUtils.isEmpty(stime)&&!StringUtils.isEmpty(etime)) {
			list.add(stime+" 00:00:00");
			list.add(etime+" 23:59:59");
			sql.append(" AND ALARM_TIME >=?");
			sql.append(" AND ALARM_TIME <=?");
		}else if(!StringUtils.isEmpty(stime)) {
			list.add(stime+" 00:00:00");
			sql.append(" AND ALARM_TIME >=?");
		}else if(!StringUtils.isEmpty(etime)) {
			list.add(etime+" 23:59:59");
			sql.append(" AND ALARM_TIME <=?");
		}
		sql.append(" ORDER BY CREATED_TIME DESC) A   ) WHERE rn BETWEEN ? AND ? ");
		list.add(mrWarningProcessing.getOffset()+1);
		list.add(mrWarningProcessing.getOffset()+mrWarningProcessing.getLimit());
		Object[]args=list.toArray();
		RowMapper<MrWarningProcessing> rowMapper = new BeanPropertyRowMapper<>(MrWarningProcessing.class);
		return jdbcTemplate.query(sql.toString(), args,rowMapper);
	}
	//总条数
	public long getPageCount(MrWarningProcessing mrWarningProcessing) {
		String stime=mrWarningProcessing.getStime();
		String etime=mrWarningProcessing.getEtime();
		List<Object> list=new ArrayList<>();
		StringBuffer sql=new StringBuffer("SELECT COUNT(1) FROM T_MR_WARNING_PROCESSING WHERE DEL_FLAG=0");
		if(!StringUtils.isEmpty(mrWarningProcessing.getAlarmItem())) {
			sql.append(" AND ALARM_ITEM=?");
			list.add(mrWarningProcessing.getAlarmItem());
		}
		if(!StringUtils.isEmpty(mrWarningProcessing.getObjType())) {
			sql.append(" AND OBJ_TYPE=?");
			list.add(mrWarningProcessing.getObjType());
		}
		if(!StringUtils.isEmpty(mrWarningProcessing.getObj())) {
			sql.append(" AND OBJ=?");
			list.add(mrWarningProcessing.getObj());
		}
		if(!StringUtils.isEmpty(stime)&&!StringUtils.isEmpty(etime)) {
			list.add(stime+" 00:00:00");
			list.add(etime+" 23:59:59");
			sql.append(" AND ALARM_TIME >=?");
			sql.append(" AND ALARM_TIME <=?");
		}else if(!StringUtils.isEmpty(stime)) {
			list.add(stime+" 00:00:00");
			sql.append(" AND ALARM_TIME >=?");
		}else if(!StringUtils.isEmpty(etime)) {
			list.add(etime+" 23:59:59");
			sql.append(" AND ALARM_TIME <=?");
		}
		Object[]args=list.toArray();
		return jdbcTemplate.queryForObject(sql.toString(),args, Long.class);
	}

	//获得监控指标下拉框数据
	public List<SysDict> getAlarmItem() {
		String sql="SELECT DICT_CODE,DICT_NAME FROM T_SYS_DICT WHERE DEL_FLAG=0 AND DICT_TYPE=\'kpi_type\'";
		RowMapper<SysDict> rowMapper = new BeanPropertyRowMapper<>(SysDict.class);
		return jdbcTemplate.query(sql,rowMapper);
	}
	//获得主题队列下拉框数据
	public List<MdTopic> getTopic() {
		String sql="SELECT ID,TOPIC_QUEUE_NAME FROM T_MD_TOPIC WHERE DEL_FLAG=0 AND STATUS=1302";
		RowMapper<MdTopic>rowMapper=new BeanPropertyRowMapper<>(MdTopic.class);
		return jdbcTemplate.query(sql,rowMapper);
	}

	//保存
	public Integer save(MrWarningProcessing mrWarningProcessing) {
		//保存
		String sql="UPDATE T_MR_WARNING_PROCESSING SET DEAL_PEOPLE=?,DEAL_WAY=?,STATUS=(SELECT DICT_CODE FROM T_SYS_DICT WHERE DICT_TYPE='warningStatus' AND DICT_NAME='已处理'),MODIFIED_TIME=? WHERE ID=?";
		return jdbcTemplate.update(sql,mrWarningProcessing.getDealPeople(),mrWarningProcessing.getDealWay(),new Date(),mrWarningProcessing.getId());
	}

	//获得监控对象类型下拉框数据
	public List<SysDict> getObjType() {
		String sql="SELECT DICT_CODE,DICT_NAME FROM T_SYS_DICT WHERE DEL_FLAG=0 AND DICT_TYPE='obj_type' AND DICT_CODE IN (SELECT OBJ_TYPE FROM T_MR_THRESHOLD_RELATION WHERE DEL_FLAG=0 GROUP BY OBJ_TYPE)";
		RowMapper<SysDict> rowMapper = new BeanPropertyRowMapper<>(SysDict.class);
		return jdbcTemplate.query(sql.toString(),rowMapper);
	}
	//获得表格展示字段
	public Map<String, Object> selectShow(MrWarningProcessing mrWarningProcessing) {
		StringBuffer sql=new StringBuffer();
		List<Object> list=new ArrayList<>();
		sql.append("SELECT * from (SELECT DICT_NAME AS kpiName FROM T_SYS_DICT WHERE DICT_TYPE = 'kpi_type' AND DICT_CODE = ?)");
		list.add(mrWarningProcessing.getAlarmItem());
		sql.append(",(SELECT DICT_NAME AS symbol FROM T_SYS_DICT WHERE DICT_TYPE = 'threshold_value_range' AND DICT_CODE = ?)");
		list.add(mrWarningProcessing.getComparisonOperators());
		sql.append(",(SELECT DICT_NAME AS objtype FROM T_SYS_DICT WHERE DICT_TYPE = 'obj_type' AND DICT_CODE = ?)");
		list.add(mrWarningProcessing.getObjType());
		String objTypeName=selectObjTypeName(mrWarningProcessing.getObjType());
		if("主题队列".equals(objTypeName)) {
			sql.append(",(SELECT TOPIC_QUEUE_NAME AS obj FROM T_MD_TOPIC WHERE ID =?)");
			list.add(mrWarningProcessing.getObj());
		}
		sql.append(",(SELECT PRINCIPAL AS people FROM T_MR_WARNING_PERSON WHERE ID=?)");
		list.add(mrWarningProcessing.getPrincipal());
		sql.append(",(SELECT DICT_NAME AS status FROM T_SYS_DICT WHERE DICT_TYPE = 'warningStatus' AND DICT_CODE = ?)");
		list.add(mrWarningProcessing.getStatus());
		Object[]args=list.toArray();
		try {
			return jdbcTemplate.queryForMap(sql.toString(), args);
		} catch (Exception e) {
			return null;
		}

	}
	//对象类型名称
	public String selectObjTypeName(String objType) {
		String sql="SELECT DICT_NAME FROM T_SYS_DICT WHERE DICT_TYPE = 'obj_type' AND DICT_CODE = ?";
		return jdbcTemplate.queryForObject(sql.toString(), String.class,objType);
	}
}
